using Microsoft.EntityFrameworkCore;
using System;
using Terra.Data.Entities.Identity;

namespace Terra.Data.SeedData.IdentitySeedData;

/// <summary>
/// 角色种子数据提供者
/// </summary>
public class RoleSeedData : ISeedDataProvider
{
    /// <summary>
    /// 系统内置角色ID常量
    /// </summary>
    public static class SystemRoleIds
    {
        /// <summary>
        /// 超级管理员角色ID
        /// </summary>
        public static readonly Guid AdminRoleId = Guid.Parse("A8D7D9BD-20F0-4AAD-A772-9F27BAE526E1");

        /// <summary>
        /// 普通用户角色ID
        /// </summary>
        public static readonly Guid UserRoleId = Guid.Parse("B9A3493D-7513-4D89-A149-28E5D0D02654");

        /// <summary>
        /// 访客角色ID
        /// </summary>
        public static readonly Guid GuestRoleId = Guid.Parse("C1EA59C0-4E36-4F31-9D4E-F5A35DDB5E82");
    }

    /// <summary>
    /// 配置角色种子数据
    /// </summary>
    /// <param name="modelBuilder">模型构建器</param>
    public void Configure(ModelBuilder modelBuilder)
    {
        var now = DateTime.UtcNow;
        var adminId = UserSeedData.SystemUserIds.AdminUserId;

        // 超级管理员角色
        modelBuilder.Entity<Role>().HasData(new Role
        {
            Id = SystemRoleIds.AdminRoleId,
            Name = "超级管理员",
            NormalizedName = "超级管理员",
            Description = "系统超级管理员，拥有所有权限",
            IsSystem = true,
            ConcurrencyStamp = Guid.NewGuid().ToString(),
            CreatedTime = now,
            CreatedBy = adminId,
            LastModifiedTime = now,
            LastModifiedBy = adminId,
            IsDeleted = false
        });

        // 普通用户角色
        modelBuilder.Entity<Role>().HasData(new Role
        {
            Id = SystemRoleIds.UserRoleId,
            Name = "普通用户",
            NormalizedName = "普通用户",
            Description = "系统普通用户，拥有基本权限",
            IsSystem = true,
            ConcurrencyStamp = Guid.NewGuid().ToString(),
            CreatedTime = now,
            CreatedBy = adminId,
            LastModifiedTime = now,
            LastModifiedBy = adminId,
            IsDeleted = false
        });

        // 访客角色
        modelBuilder.Entity<Role>().HasData(new Role
        {
            Id = SystemRoleIds.GuestRoleId,
            Name = "访客",
            NormalizedName = "访客",
            Description = "系统访客，拥有只读权限",
            IsSystem = true,
            ConcurrencyStamp = Guid.NewGuid().ToString(),
            CreatedTime = now,
            CreatedBy = adminId,
            LastModifiedTime = now,
            LastModifiedBy = adminId,
            IsDeleted = false
        });
    }

    /// <summary>
    /// 获取提供者优先级（在用户种子数据之后）
    /// </summary>
    public int Order => 20;
}